package edu.wit.system.controller;


import edu.wit.common.result.R;
import edu.wit.common.utils.ServletUtils;
import edu.wit.common.web.PageResult;
import edu.wit.common.web.Result;
import edu.wit.system.domain.AffairsApplyWorkflowCompany;
import edu.wit.system.domain.dto.AffairsApplyWorkflowDto;
import edu.wit.system.domain.vo.AffairsApplyWorkflowExamineVo;
import edu.wit.system.domain.vo.AffairsApplyWorkflowVo;
//import edu.wit.system.feign.WagelFeign;
import edu.wit.system.domain.vo.PersonApplyStatusVo;
import edu.wit.system.service.AffairsApplyWorkflowCompanyService;
import edu.wit.system.service.AffairsApplyWorkflowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author lee
 */
@RestController
@RequestMapping("/sys/affairsApplyWorkflow")
@Api(tags = "流程申请")
@Validated
public class AffairsApplyWorkflowController {
    @Autowired
    private AffairsApplyWorkflowService affairsApplyWorkflowService;

    @Autowired
    private AffairsApplyWorkflowCompanyService affairsApplyWorkflowCompanyService;

//    @Autowired
//    WagelFeign personnelFeign;
    /**
     * 我的申请列表
     */
    @ApiOperation(value = "我的申请列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",required = true,dataType = "Integer",paramType = "form",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,dataType = "Integer",paramType = "form",defaultValue = "10"),
            @ApiImplicitParam(name="orderByColumn" ,value="排序行",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name="isAsc" ,value="排序的方向 desc 或者 asc",dataType = "String",paramType = "query"),
    })
    @GetMapping("/getApplyList")
    public PageResult<AffairsApplyWorkflowDto> getApplyList(@RequestParam @ApiIgnore Map<String, Object> params,
                                                            @RequestHeader(required = false) String serviceCorporationId){
        if (serviceCorporationId == null) {
            //项目方进入
            if (params.get("corporation") == null) {
                //不做处理返回
                /**
                 * 现在要求不传 corporation就查询所有的！
                 */
                serviceCorporationId = null;
//                return new PageResult<>(0L,200,null,new Date());
            } else {
                serviceCorporationId = String.valueOf(params.get("corporation"));
            }
        }
        if (params.get("workflowType").equals("劳务代发审批")) {
            params.put("companyId", serviceCorporationId);
        } else {
            params.put("applyUserId", ServletUtils.getRequest().getHeader("user_id"));
        }
        if (params.get("status") != null) {
            if (params.get("status").equals("-1")) {
                params.remove("status");
            }
        }
        return affairsApplyWorkflowService.getApplyList(params);
    }


    /**
     * 我的审核列表
     */
    @ApiOperation(value = "我的审核列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",required = true,dataType = "Integer",paramType = "form",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,dataType = "Integer",paramType = "form",defaultValue = "10"),
            @ApiImplicitParam(name="orderByColumn" ,value="排序行",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name="isAsc" ,value="排序的方向 desc 或者 asc",dataType = "String",paramType = "query"),
    })
    @GetMapping("/getExamineList")
    public PageResult<AffairsApplyWorkflowDto> getExamineList(@RequestParam @ApiIgnore Map<String, Object> params){
        System.out.println("我的审批列表获取");
        return affairsApplyWorkflowService.getExamineList(params);
    }

    /**
     * 信息
     */
    @ApiOperation(value = "详细",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "ID",required = true,dataType = "Long",paramType = "form"),
    })
    @GetMapping("/info")
    public Result<AffairsApplyWorkflowDto> info(@RequestParam("applyId") Long applyId){
        System.out.println("我的流程详细获取");
        AffairsApplyWorkflowDto affairsApplyWorkflowDto = affairsApplyWorkflowService.getById(applyId);
        return Result.success(affairsApplyWorkflowDto, "操作成功");
    }

    /**
     * 申请
     */
    @ApiOperation(value = "申请",httpMethod = "POST")
    @PostMapping("/apply")
    // @Log(title = "申请")
    public Result<Boolean> apply(@RequestBody(required = false) @Valid AffairsApplyWorkflowVo affairsApplyWorkflowVo){
        affairsApplyWorkflowService.apply(affairsApplyWorkflowVo);
        return Result.success(true, "申请成功");
    }

    /**
     * 撤销
     */
    @ApiOperation(value = "撤销",httpMethod = "POST")
    // @Log(title = "撤销")
    @GetMapping("/cancel")
    public Result<Boolean> cancel(@RequestParam("applyId") Long applyId){
        affairsApplyWorkflowService.cancel(applyId);
        return Result.success(true, "撤销成功");
    }

    /**
     * 审核
     * 		   1.applyNodeId
     * 		   2.type(1-同意；2-驳回)
     * 		   3.examineReason
     * 		   4.examineUserId(移动端需要，pc端不需要传)
     */
    @ApiOperation(value = "审核",httpMethod = "POST")
    @PostMapping("/examine")
    // @Log(title = "审核")
    public Result<Boolean> examine(@RequestBody @Valid AffairsApplyWorkflowExamineVo affairsApplyWorkflowExamineVo){
        System.out.println("我的流程审核获取");
        affairsApplyWorkflowService.examine(affairsApplyWorkflowExamineVo);
        return Result.success(true, "审核成功");
    }

    /**
     * 工人审核驳回需要传personId
     * @param affairsApplyWorkflowExamineVo 流程
     * @param workerId 工人id
     */
    @ApiOperation(value = "工人审核驳回",httpMethod = "POST")
    @PostMapping("/workerExamine")
    // @Log(title = "审核")
    public Result<Boolean> workerExamine(@RequestBody @Valid AffairsApplyWorkflowExamineVo affairsApplyWorkflowExamineVo,String workerId){
        System.out.println("我的流程审核获取");
        affairsApplyWorkflowService.workerExamine(affairsApplyWorkflowExamineVo,workerId);
        return Result.success(true, "审核成功");
    }
    /**
     * 多个审核
     * 		   1.applyNodeId
     * 		   2.type(1-同意；2-驳回)
     * 		   3.examineReason
     * 		   4.examineUserId(移动端需要，pc端不需要传)
     */
    @ApiOperation(value = "多个审核(pc端不需要传examineId)",httpMethod = "POST")
    @PostMapping("/multipleExamine")
    // @Log(title = "审核")
    public Result<Boolean> multipleExamine(@RequestBody @Valid List<AffairsApplyWorkflowExamineVo> affairsApplyWorkflowExamineVos){
        System.out.println("我的流程审核(多个审批)获取");
        affairsApplyWorkflowService.multipleExamine(affairsApplyWorkflowExamineVos);
        return Result.success(true, "审核成功");
    }

    /**
     * 月工资统计页面 获取指定劳务公司的申请流程信息
     */
    @ApiOperation(value = "月工资统计页面 获取指定劳务公司的申请流程信息",httpMethod = "GET")
    @GetMapping("/getCompanyApplyList/{companyId}")
    public R getCompanyApplyList(@PathVariable Long companyId){
        System.out.println("月工资统计页面 获取指定劳务公司的申请流程信息");
        List<AffairsApplyWorkflowCompany> affairsApplyWorkflowCompanyList = affairsApplyWorkflowCompanyService.selectByCompanyId(companyId+"");
        return R.ok().data("affairsApplyWorkflowCompanyList",affairsApplyWorkflowCompanyList);
    }
    /**
     * 月劳务代发统计页面 获取指定项目方的劳务代发申请流程信息
     */
    @ApiOperation(value = "月劳务代发统计页面 获取指定项目方的劳务代发申请流程信息",httpMethod = "GET")
    @GetMapping("/getProjectApplyList")
    public R getProjectApplyList(){
        System.out.println("月劳务代发统计页面 获取指定项目方的劳务代发申请流程信息");
        List<AffairsApplyWorkflowCompany> affairsApplyWorkflowProjectList = affairsApplyWorkflowCompanyService.selectByCompanyId();
        return R.ok().data("affairsApplyWorkflowProjectList",affairsApplyWorkflowProjectList);
    }

    @ApiOperation(value = "Test远程调用",httpMethod = "GET")
    @GetMapping("/Test")
    public R Test(){
        List<Long> personIds = new ArrayList<>();
        personIds.add(4113L);
//        personnelFeign.commitUpdate(personIds,"2021-07",1);
        return R.ok();
    }

    @ApiOperation(value = "获取工人所在班组所有人的流程状态")
    @GetMapping("/getObjectionListByPersonId")
    public R getObjectionListByPersonId(String personId,String applyStatus) {
        List<PersonApplyStatusVo> personApplyStatusVos = affairsApplyWorkflowService.selectObjectionListByPersonId(personId,applyStatus);
        return R.ok().data("data",personApplyStatusVos);
    }
}
